#include "gethistoryresponse.h"


#include <QDebug>


GetHistoryResponse::Item::Item(qint64 receiverId,
                               qint64 created,
                               const QString &extdata,
                               qint64 senderId,
                               qint64 mid,
                               int type,
                               qint64 chid,
                               const QString &text):
    m_receiverId(receiverId),
    m_created(created),
    m_extdata(extdata),
    m_senderId(senderId),
    m_mid(mid),
    m_mType(type),
    m_chid(chid),
    m_text(text)
{

}


GetHistoryResponse::GetHistoryResponse(QObject *parent) :
    Network::JSONListResponse(parent)
{
}


GetHistoryResponse::~GetHistoryResponse()
{
    qDeleteAll(m_items);
}



/**
 * @brief GetHistoryResponse::parse
 * @param list
 */
void GetHistoryResponse::parse(const QVariantList &items)
{
    bool ok;
    foreach (QVariant item, items) {
        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The field of \"map\" isn't a map"));
            return;
        }

        QVariantMap map = item.toMap();
        if (!map.contains("cmd")) {
            setError(Network::Response::Property, "The field of \"cmd\" is missing");
            return;
        }

        if (!map.contains("data")) {
            setError(Network::Response::Property, "The field of \"data\" is missing");
            return;
        }

        if (!map.contains("r")) {
            setError(Network::Response::Property, "The field of \"r\" is missing");
            return;
        }

        QVariant messageVariant = map.value("data");

        if (messageVariant.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The field of \"data\" isn't a map"));
            return;
        }

        QVariantMap messageMap = messageVariant.toMap();

        // to
        if (!messageMap.contains("to")) {
            setError(Network::Response::Property, "The field of \"to\" is missing");
            continue;
        }

        QVariant messageTo = messageMap.value("to");
        qint64 receiverId = messageTo.toString().toLongLong(&ok);
        if (!ok) {
            // qDebug() << "The value of the field of \"to\" could not converted to int";
            continue;
        }

        if (receiverId <= 0) {
            // qDebug() << "The value of receiverId is invalid";
            continue;
        }

        // time created
        if (!messageMap.contains("tcreate")) {
            setError(Network::Response::Property, "The field of \"tcreate\" is missing");
            return;
        }

        QVariant messageCreatedTime = messageMap.value("tcreate");
        qint64 createdTime = messageCreatedTime.toString().toLongLong(&ok);
        if (!ok) {
            // qDebug() << "The value of the field of \"tcreate\" could not converted to int";
            continue;
        }

        // extdata
        if (!messageMap.contains("extdata")) {
            setError(Network::Response::Property, "The field of \"extdata\" is missing");
            return;
        }

        QVariant messageExtData = messageMap.value("extdata");
        if (messageExtData.type() != QVariant::String) {
            setError(Network::Response::Property, "The value of the field of \"extdata\" is not compatible");
            return;
        }

        QString extdata = messageExtData.toString();

        // from
        if (!messageMap.contains("from")) {
            setError(Network::Response::Property, "The field of \"from\" is missing");
            return;
        }

        QVariant messageFrom = messageMap.value("from");
        qint64 senderId = messageFrom.toString().toLongLong(&ok);
        if (!ok) {
            // qDebug() << "The value of the field of \"from\" could not converted to int";
            continue;
        }

        if (senderId <= 0) {
            // qDebug() << "The value of senderId is invalid";
            continue;
        }

        // mid
        if (!messageMap.contains("mid")) {
            setError(Network::Response::Property, "The field of \"mid\" is missing");
            return;
        }

        QVariant messageMid = messageMap.value("mid");
        qint64 mid = messageMid.toString().toLongLong(&ok);
        if (!ok) {
            // qDebug() << "The value of the field of \"mid\" could not converted to int";
            continue;
        }

        // type
        if (!messageMap.contains("mtype")) {
            setError(Network::Response::Property, "The field of \"type\" is missing");
            return;
        }

        QVariant messageType = messageMap.value("mtype");
        int type = messageType.toString().toInt(&ok);
        if (!ok) {
            // qDebug() << "The value of the field \"mtype\" could not converted to int";
            continue;
        }

        // chid
        if (!messageMap.contains("chid")) {
            setError(Network::Response::Property, "The field of \"chid\" is missing");
            return;
        }

        QVariant conversationIdVariant = messageMap.value("chid");
        qint64 conversationId = conversationIdVariant.toString().toLongLong(&ok);
        if (!ok) {
            // qDebug() << "The value of the field of \"chid\" could not converted to int ";
            continue;
        }

        if (conversationId <= 0) {
            // qDebug() << "The value of conversationId is invalid";
            continue;
        }

        // message
        if (!messageMap.contains("msg")) {
            setError(Network::Response::Property, "The field of \"msg\" is missing");
            return;
        }

        QVariant messageContentVariant = messageMap.value("msg");
        if (messageContentVariant.type() != QVariant::String) {
            setError(Network::Response::Property, "The value of \"message\" is not compatible");
            return;
        }

        QString messageContent = messageContentVariant.toString();

        m_items.append(new Item(receiverId,
                                createdTime,
                                extdata,
                                senderId,
                                mid,
                                type,
                                conversationId,
                                messageContent));

    }
}
